package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"novel/models/cache"
	"novel/models/mysql"
	"time"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentShowLogic {
	return &CommentShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 作品评论详情
func (l *CommentShowLogic) CommentShow(in *novelrpc.CommentShowRequest) (*novelrpc.CommentShowResponse, error) {
	// todo: add your logic here and delete this line

	var comment []*novelrpc.CommentShow
	key := fmt.Sprintf("comment_show_%d", in.NovelId)
	//var comment mysql.Comment
	result, _ := cache.Rdb.Get(l.ctx, key).Result()
	if result == "" {
		comment = mysql.CommentShow(mysql.DB, in)
		marshal, _ := json.Marshal(comment)
		cache.Rdb.Set(l.ctx, key, marshal, time.Minute*15)
	} else {
		json.Unmarshal([]byte(result), &comment)
	}

	return &novelrpc.CommentShowResponse{
		Show: comment,
	}, nil
}
